package yxk.form;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.FormShowParameter;
import kd.bos.form.control.Control;
import kd.bos.form.control.events.BeforeClickEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.orm.util.CollectionUtils;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.hr.hbp.common.util.HRJSONUtils;
import kd.hr.hom.business.domain.repository.common.HomCommonRepository;
import kd.sdk.plugin.Plugin;
import yxk.list.odc.homs.util.OnboardUtil;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 动态表单 批量确认入职 点击确认按钮时调用第三方接口
 */
public class OnbrdConfirmMultiEditFormPlugin extends AbstractFormPlugin implements Plugin {

    private static final String BTN_CONFIRM = "btn_confirm";

    private static final String ENTRY_ENTITY = "entryentity";
    private OnboardUtil onboardUtil=new OnboardUtil();
    public void registerListener(EventObject eventObject) {
        super.registerListener(eventObject);
        this.addClickListeners("btn_confirm");
    }

    @Override
    public void beforeClick(BeforeClickEvent evt) {
        super.beforeClick(evt);
        if (evt.getSource() instanceof Control) {
            Control control = (Control) evt.getSource();
            String key = control.getKey();
            if (BTN_CONFIRM.equals(key)) {
                /*FormShowParameter formShowParameter = this.getView().getFormShowParameter();
                Long id = HRJSONUtils.getLongValOfCustomParam(formShowParameter.getCustomParam("id"));
                DynamicObject dynamicObject = HomCommonRepository.queryDynamicObjectByPk("hom_onbrdinfo", "checkinstatus", id);
                System.out.println(dynamicObject);*/

                DynamicObjectCollection dynamicObjectCollection = this.getModel().getEntryEntity(ENTRY_ENTITY);

                List<Object> idList = dynamicObjectCollection.stream().map(item -> item.get("id")).collect(Collectors.toList());
                DynamicObject[] hom_onbrdinfoCollection = BusinessDataServiceHelper.load(
                        "hom_onbrdinfo", "id,candidatenumber,probationtime,effectdate,offernumber",
                        new QFilter[]{new QFilter("id", QCP.in, idList)});

                List<Object> errorIds=new ArrayList<>();
                for (DynamicObject object : hom_onbrdinfoCollection) {
                    String offerNumber = object.getString("offernumber");
                    if(offerNumber != null && !"".equals(offerNumber)) {
                        String candidatenumber = object.getString("candidatenumber");//候选人编码  employeeno工号
                        Integer probationtime = object.getInt("probationtime");//试用期
                        //String offerNumber = dynamicObject.getString("offernumber");//offer编号
                        Date effectdate = object.getDate("effectdate");

                        String token = onboardUtil.getZhaoPinToken();

                        Map<String, Object> result = onboardUtil.getHired(token, candidatenumber, probationtime, effectdate);
                        Boolean isSuccess = (Boolean) result.get("success");
                        if (!isSuccess) {
                            errorIds.add(candidatenumber);
                        }
                    }
                }
                if(!CollectionUtils.isEmpty(errorIds)){
                    this.getView().showErrorNotification("工号为："+errorIds.toString()+"的员工入职失败！");
                }
            }
        }
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        if (evt.getSource() instanceof Control) {
            Control control = (Control) evt.getSource();
            String key = control.getKey();
            if (BTN_CONFIRM.equals(key)) {
                FormShowParameter formShowParameter = this.getView().getFormShowParameter();
                Long id = HRJSONUtils.getLongValOfCustomParam(formShowParameter.getCustomParam("id"));
                DynamicObject dynamicObject = HomCommonRepository.queryDynamicObjectByPk("hom_onbrdinfo", "checkinstatus", id);
                System.out.println(dynamicObject);
            }
        }
    }
}
